import okhttp3.*;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class Demo {



    private static final Logger LOGGER = LoggerFactory.getLogger(Demo.class);
    private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    private static final OkHttpClient httpClient = new OkHttpClient.Builder()
            .connectTimeout(40, TimeUnit.SECONDS)
            .readTimeout(40, TimeUnit.SECONDS)
            .build();


    public static JSONObject post(String url, JSONObject body) {
        RequestBody requestBody = RequestBody.create(JSON, body.toString());
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        return handleRequest(request);
    }

    private static void handleAsyncRequest(Request request, Callback callback) {
        httpClient.newCall(request).enqueue(callback);
    }

    public static JSONObject postNotBody(String token, String url) {
        Request request = new Request.Builder()
                .url(url)
                .post(RequestBody.create(JSON, new JSONObject().toString()))
                .header("Authorization", token)
                .build();
        return handleRequest(request);
    }

    private static JSONObject handleRequest(Request request) {
        try {
            Response response = httpClient.newCall(request).execute();
            if (response != null) {
                String st = new String(response.body().bytes(), "utf-8");
                return new JSONObject(st);
            }else {
                throw new FruitKingNBException(NBStatus.HTTP_REQUEST_ERROR);
            }
        } catch (IOException e) {
            LOGGER.info("http request error::{}", e.getMessage());
            throw new FruitKingNBException(NBStatus.HTTP_REQUEST_ERROR);
        }
    }
    public static JSONObject get(String Authorization, String url) {
        Request request = new Request.Builder()
                .header("Authorization", Authorization)
//                .header("Connection","keep-alive")
//                .header("Accept-Encoding","gzip, deflate, br")
//                .header("Accept","*/*")
//                .header("Testdata-Agent","PostmanRuntime/7.24.1")
                .header("Host","<calculated when request is sent>")
                .header("Authorization","Bearer e984c2e31917b8f7e40c57230497644ed48fbace72a26e542d77b070501ba467")
                .header("Cookie","uid=rBACIl7DTBnAfRKgWiALAg==; _ga=GA1.2.567674898.1589857720; intercom-id-xbtsuf77=e783b7c4-de5e-494b-a6c9-f45d78baac5e; intercom-session-xbtsuf77=; mp_72ddb7dd79e1c48afb3e27e0efd70a74_mixpanel=%7B%22distinct_id%22%3A%20%221722ae795434d9-037e3d871b94f3-26096c2f-144000-1722ae7954448e%22%2C%22%24device_id%22%3A%20%221722ae795434d9-037e3d871b94f3-26096c2f-144000-1722ae7954448e%22%2C%22%24initial_referrer%22%3A%20%22%24direct%22%2C%22%24initial_referring_domain%22%3A%20%22%24direct%22%7D; remember_token=63009470-4880-4bb8-adfe-0581ca97fac7; remember_team_guid=66f9f25eee0a44d18282ffdcd0b33e1f; _tower2_session=a0235dcba2e7c468616100664e0020b2; Hm_lvt_e1fa918d304786452f7d6936febd93c6=1589869879,1589870117,1589966127,1590134154; Hm_lpvt_e1fa918d304786452f7d6936febd93c6=1590134154; _gid=GA1.2.2863694.1590134155")
                .url(url)
                .build();
        return handleRequest(request);
    }
    public static void main(String[] args) {
//        JSONArray jsonArray = new JSONArray();
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("username", "admin");
//        jsonObject.put("password", "111111");
//        jsonArray.put(jsonObject);
//        JSONObject data = new JSONObject();
//        data.put("data", jsonArray);
//String url="";
//        LOGGER.info(post(url,jsonObject).toString());

//
        String token = "f23d38bf732a8290158ba2701fa3e079cb6c4847eea955ce2561ef8550bbcf72";
        LOGGER.info(get(token,"https://tower.im/api/v1/user").toString());
    }
}
